package map.test;


import java.util.TreeMap;
import java.util.function.BiConsumer;

/*
统计字符串中每个字符出现的次数，并按格式输出
    a(5)b(4)c(2)
 */
public class MapTest1 {
    public static void main(String[] args) {
        String s = "aabbfcccbxss";
        TreeMap<Character,Integer> tm = new TreeMap<>();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if(tm.containsKey(c)){
                tm.put(c,tm.get(c)+1);
            }else{
                tm.put(c,1);
            }
        }
        //用StringBuilder更快
        StringBuilder sb = new StringBuilder();
        tm.forEach((k, v) -> sb.append(k).append("(").append(v).append(")"));
        System.out.println(sb);
    }
}
